package com.catchdays;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.util.SparseArray;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.SherlockActivity;
import com.catchdays.constant.RequestCode;
import com.catchdays.db.DatabaseHelper;
import com.catchdays.db.model.CatchDaysModel;
import com.catchdays.main.CatchDaysListAdapter;
import com.catchdays.main.SeparatedListAdapter;
import com.googlecode.androidannotations.annotations.AfterViews;
import com.googlecode.androidannotations.annotations.EActivity;
import com.googlecode.androidannotations.annotations.ItemClick;
import com.googlecode.androidannotations.annotations.OptionsItem;
import com.googlecode.androidannotations.annotations.OptionsMenu;
import com.googlecode.androidannotations.annotations.OrmLiteDao;
import com.googlecode.androidannotations.annotations.ViewById;
import com.j256.ormlite.dao.Dao;

@EActivity(R.layout.activity_main)
@OptionsMenu(R.menu.main)
public class MainActivity extends SherlockActivity implements ActionBar.OnNavigationListener {
	private static final String TAG = "MainActivity";

	@OrmLiteDao(helper = DatabaseHelper.class, model = CatchDaysModel.class)
	Dao<CatchDaysModel, Integer> catchDaysDao;

	@ViewById
	ListView catchDaysListView;

	@AfterViews
	void init() {
		Context context = getSupportActionBar().getThemedContext();
		ArrayAdapter<CharSequence> menuList = ArrayAdapter.createFromResource(context, R.array.listTypes, R.layout.sherlock_spinner_item);
		menuList.setDropDownViewResource(R.layout.sherlock_spinner_dropdown_item);

		getSupportActionBar().setDisplayShowTitleEnabled(false);
		getSupportActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
		getSupportActionBar().setListNavigationCallbacks(menuList, this);
	}

	@OptionsItem(R.id.menu_add)
	public void menuAdd() {
		Intent intent = new Intent(this, AddActivity_.class);
		startActivity(intent);
	}

	@OptionsItem(R.id.menu_info)
	public void menuInfo() {
		Intent intent = new Intent(this, InfoActivity_.class);
		startActivity(intent);
	}

	@OptionsItem(R.id.menu_help)
	public void menuHelp() {
		Intent intent = new Intent(this, HelpActivity_.class);
		startActivity(intent);
	}

	@OptionsItem(R.id.menu_preferences)
	public void menuPreferences() {
		Intent intent = new Intent(this, PreferencesActivity_.class);
		startActivity(intent);
	}

	@ItemClick
	public void catchDaysListView(CatchDaysModel catchDays) {
		Intent intent = new Intent(this, ViewActivity_.class);
		intent.putExtra("id", catchDays.getId());

		startActivityForResult(intent, RequestCode.VIEW);
	}

	@Override
	public boolean onNavigationItemSelected(int itemPosition, long itemId) {
		loadMainList();

		return true;
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
	}

	public void loadMainList() {
		String[] months = getResources().getStringArray(R.array.months);

		try {
			SparseArray<List<CatchDaysModel>> catchDaysArray = new SparseArray<List<CatchDaysModel>>();
			List<CatchDaysModel> catchDaysList = catchDaysDao.queryForAll();

			int headerMonth = -1;

			for (CatchDaysModel catchDays : catchDaysList) {
				int anniversaryMonth = catchDays.getAnniversaryMonth();

				if (headerMonth == -1) {
					headerMonth = anniversaryMonth;
					catchDaysArray.put(headerMonth, new ArrayList<CatchDaysModel>());
				} else if (headerMonth != anniversaryMonth) {
					headerMonth = anniversaryMonth;
					catchDaysArray.put(headerMonth, new ArrayList<CatchDaysModel>());
				}

				catchDaysArray.get(headerMonth).add(catchDays);
			}

			SeparatedListAdapter separatedAdapter = new SeparatedListAdapter(this);

			for (int i = 0; i < catchDaysArray.size(); i++) {
				int key = catchDaysArray.keyAt(i);

				CatchDaysListAdapter listAdapter = new CatchDaysListAdapter(this);
				listAdapter.setCatchDaysList(catchDaysArray.get(key));
				separatedAdapter.addSection(months[key - 1], listAdapter);
			}

			catchDaysListView.setAdapter(separatedAdapter);
		} catch (SQLException e) {
			Log.e(TAG, "CatchDaysDao.create() Error", e);
		}
	}
}
